package com.ylyq.ylxx.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.util.Tools;
import lombok.SneakyThrows;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @SneakyThrows
    @Override
    public void insertFill(MetaObject metaObject) {

        this.setFieldValByName("createdBy", FilterContextHandler.getUserID(), metaObject);
        this.setFieldValByName("createdTime",  new Date(), metaObject);
        this.setFieldValByName("updatedBy", FilterContextHandler.getUserID(), metaObject);
        this.setFieldValByName("updatedTime", new Date(), metaObject);
        this.setFieldValByName("delFlag", 0, metaObject);

        if (metaObject.hasSetter("id") && metaObject.getValue("id") == null) {
            this.setFieldValByName("id", Tools.getUUID(), metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {

        this.setFieldValByName("updatedTime", new Date(), metaObject);
        this.setFieldValByName("updatedBy", FilterContextHandler.getUserID(), metaObject);
    }
}
